const { src, dest, series, parallel, gulp } = require('gulp');
const $ = require('gulp-load-plugins')();
const exec = require('gulp-exec')
var path = require('path');
var del = require('del');

var distPath = path.resolve('./dist');
var version = ''; // 版本号
var versionPath = ''; // 版本号路径
var env = ''; // 运行环境

// 创建版本号(年月日时分)
(function () {
  var d = new Date();
  var yy = d.getFullYear().toString().slice(2);
  var MM = d.getMonth() + 1 >= 10 ? (d.getMonth() + 1) : '0' + (d.getMonth() + 1);
  var DD = d.getDate() >= 10 ? d.getDate() : '0' + d.getDate();
  var h = d.getHours() >= 10 ? d.getHours() : '0' + d.getHours();
  var mm = d.getMinutes() >= 10 ? d.getMinutes() : '0' + d.getMinutes();
  version = yy + MM + DD + h + mm;
  versionPath = distPath + '/' + version;
})();

// 编译
function build (cb) {
  // shell.task([ 'node build/build.js' ]);

  var options = {
    continueOnError: true, // default = false, true means don't emit error event
    pipeStdout: false, // default = false, true means stdout is written to file.contents
    customTemplatingThing: "test" // content passed to lodash.template()
  };
  var reportOptions = {
    err: true, // default = true, false means don't write err
    stderr: true, // default = true, false means don't write stderr
    stdout: true // default = true, false means don't write stdout
  };
  return src('./build/build.js')
    // .pipe(exec('node <%= file.path %> ', options))
    .pipe(exec('node build/build.js', options))
    .pipe(exec.reporter(reportOptions));

  // cd();
  cb();
}


// 创建版本号目录
function createVersionCatalog () {
  return src(`${distPath}/static/**/*`)
    .pipe(dest(`${versionPath}/static/`))
}


// 替换${versionPath}/static/js/manifest.js window.SITE_CONFIG.cdnUrl占位变量
function replaceCdnUrl () {
  return src(`${versionPath}/static/js/manifest.js`)
    .pipe($.replace(new RegExp(`"${require('./config').build.assetsPublicPath}"`, 'g'), 'window.SITE_CONFIG.cdnUrl + "/"'))
    .pipe(dest(`${versionPath}/static/js/`));
}


// 替换${versionPath}/static/config/index-${env}.js window.SITE_CONFIG['version']配置变量
function replaceVersion () {
  return src(`${versionPath}/static/config/index-${env}.js`)
    .pipe($.replace(/window.SITE_CONFIG\['version'\] = '.*'/g, `window.SITE_CONFIG['version'] = '${version}'`))
    .pipe($.replace(/window.SITE_CONFIG.version=""/g, `window.SITE_CONFIG.version="${version}"`))
    .pipe(dest(`${versionPath}/static/config/`));
}

// 合并${versionPath}/static/config/[index-${env}, init].js 至 ${distPath}/config/index.js
function concatConfig () {
  return src([`${versionPath}/static/config/index-${env}.js`, `${versionPath}/static/config/init.js`])
    .pipe($.concat('index.js'))
    .pipe(dest(`${distPath}/config/`))
}

function removeCssToStatic () {
  return src(`${distPath}/css/*`)
    .pipe(dest(`${versionPath}/css/`))
}

// 清空
function clean (cb) {
  del([versionPath]);
  cb();
}

function setEnv (cb) {
  // 获取环境配置
  env = process.env.npm_config_qa ? 'qa' : process.env.npm_config_uat ? 'uat' : 'prod';
  cb();
}

function afterBuild (cb) {
  // 清除, 编译 / 处理项目中产生的文件
  del([`${distPath}/css`, `${distPath}/static`, `${versionPath}/static/config`]);
  cb();
}

exports.build = build;
exports.default = series(
  clean,
  setEnv,
  build,
  series(createVersionCatalog,
    replaceCdnUrl,
    replaceVersion,
    concatConfig,
    removeCssToStatic
  ),
  afterBuild
);
